package com.hotel.controller;

import com.hotel.pojo.Check;
import com.hotel.pojo.Home;
import com.hotel.pojo.Rooms;
import com.hotel.service.CheckService;
import com.hotel.service.HomeService;
import com.hotel.service.RoomServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/check")
@RequiredArgsConstructor
public class CheckController {


    private final CheckService checkService;
    private final HomeService homeService;

    @RequestMapping("/add1")
    public ModelAndView add1(){
        ModelAndView mv = new ModelAndView();
        ArrayList<Home> home = homeService.queryHomeByStatus("已预定");
        mv.addObject("list",home);
        mv.setViewName("check_add");
        return mv;
    }

    @RequestMapping("/add")
    public String add(Check check){

        check.setCheckInTime(new Date());
        checkService.addCheck(check);
        Home home  = new Home();
        home.setState("已入住");
        home.setId(Integer.parseInt(check.getRoomId()));
      homeService.updateH_TypeById(home);

        return "redirect:/check/list";
    }


    @RequestMapping("/delete")
    public String delete(Integer id){
        Check check = checkService.queryCheckById(id);
        if(!check.getStatus().equals("已入住")){
            return "redirect:/check/list";
        }else {
            return "error";
        }

    }

    @RequestMapping("/list")
    public ModelAndView list(){
        ModelAndView mv = new ModelAndView();
        List<Check> checkList=checkService.queryAllChecks();
        List<Check> collected = checkList.stream()
                .map(check -> {
                    int i = Integer.parseInt(check.getRoomId());
                    Home home = homeService.queryHomeById(i);
                    check.setRoomNumber(home.getNum());
                    return check;
                }).collect(Collectors.toList());
        mv.addObject("checkList",collected);
        mv.setViewName("check_list");
        return mv;
    }

    @RequestMapping("/update1")
    public ModelAndView update1(Integer id){
        ModelAndView mv = new ModelAndView();
        ArrayList<Home> home = homeService.queryHomeByStatus("已预定");
        Check check = checkService.queryCheckById(id);
        check.setRoomNumber(homeService.queryHomeById(Integer.parseInt(check.getRoomId())).getNum());
        mv.addObject("g",check);
        mv.addObject("list",home);
        mv.setViewName("check_update");
        return mv;
    }

    @RequestMapping("/update2")
    public String update2(Check check){
        Check before = checkService.queryCheckById(check.getId());
        if(!Objects.equals(check.getRoomId(), before.getRoomId())){
            Home home  = new Home();
            home.setState("未打扫");
            home.setId(Integer.parseInt(before.getRoomId()));
            homeService.updateH_TypeById(home);
        }
        checkService.updateCheckById(check);
        return ("redirect:/check/list");
    }
    @RequestMapping("/checkOut")
    public String checkOut(Integer id){
        Check check = checkService.queryCheckById(id);
        check.setCheckOutTime(new Date());
        checkService.updateStatusAndTimeById(check);
        Home home  = new Home();
        home.setState("未打扫");
        home.setId(Integer.parseInt(check.getRoomId()));
        homeService.updateH_TypeById(home);
        return "redirect:/check/list";
    }

    @RequestMapping("/find")
    public ModelAndView find(String roomNumber){
        ModelAndView mv = new ModelAndView();

        if (roomNumber != null && !roomNumber.trim().isEmpty()) {
            Check check = checkService.queryCheckByRoomNumber(roomNumber);

            if (check != null) {
                String roomId = check.getRoomId();
                if (roomId != null) {
                    Home home = homeService.queryHomeById(Integer.parseInt(roomId));
                    if (home != null) {
                        check.setRoomNumber(home.getNum());
                    } else {
                        mv.addObject("error", "未找到对应的房间信息");
                    }
                } else {
                    mv.addObject("error", "房间ID为空");
                }
            } else {
                mv.addObject("error", "未查询出结果");
            }

            List<Check> checkList = new ArrayList<>();
            if (check != null) {
                checkList.add(check);
            } else {
                checkList = checkService.queryAllChecks();
            }

            mv.addObject("checkList", checkList);
        } else {
            List<Check> checkList = checkService.queryAllChecks();
            mv.addObject("checkList", checkList);
            mv.addObject("error", "请输入有效的房间号");
        }

        mv.setViewName("check_list");
        return mv;
    }
    @RequestMapping("/show")
    public ModelAndView show(Integer id){
        ModelAndView mv = new ModelAndView();
        Check check=checkService.queryCheckById(id);
        check.setRoomNumber(homeService.queryHomeById(Integer.parseInt(check.getRoomId())).getNum());
        mv.addObject("check",check);
        mv.setViewName("check_show");
        return mv;
    }


    @RequestMapping("/exit_list")
    public ModelAndView exitlist(){
        ModelAndView mv = new ModelAndView();
        List<Check> checkList=checkService.queryAllChecks();
        List<Check> collected = checkList.stream()
                .map(check -> {
                    int i = Integer.parseInt(check.getRoomId());
                    Home home = homeService.queryHomeById(i);
                    check.setRoomNumber(home.getNum());
                    return check;
                }).collect(Collectors.toList());
        mv.addObject("checkList",collected);
        mv.setViewName("exitHome_list");
        return mv;
    }

}
